<?php
namespace Admin\Controller;
use Think\Controller;
class ZhuanjiaController extends AdminController {
    public function index(){
        $zMO = M('zhuanjia');  
        $tCount = $zMO->count();
        $tPage = new \Think\Page($tCount,25);
        $tShow = $tPage->show();
        $tData = $zMO->order('id desc')->limit($tPage->firstRow.','.$tPage->listRows)->select();
        $this->assign('tPage',$tShow);// 赋值分页输出
        $this->assign('tData' , $tData);
        $this->display();

    }
    public function add(){
        $tTime = time(); 
        if(IS_POST){
            $pName = I('name' , '' , 'htmlspecialchars'); 
            $pDescription = I('description' , '' , 'htmlspecialchars'); 
            $pDepartment = I('department' , '' , 'htmlspecialchars'); 
            $pIntroduction = I('introduction' , '' , 'htmlspecialchars'); 
            $pOccupation = I('occupation' , '' , 'htmlspecialchars'); 
            $pLitpic = I('litpic', '', 'htmlspecialchars,trim');
            if(empty($pName)){
                $this->error('专家名称能为空');
            }
            if(empty($pIntroduction)){
                $this->error('介绍不能为空');
            }
            $tData = array(
                'name' => $pName,
                'description' => $pDescription,
                'introduction' => $pIntroduction,
                'occupation' => $pOccupation,
                'department' => $pDepartment,
                'created' => $tTime,
                'litpic' => $pLitpic,
            );
            $tMO = M('zhuanjia');
            if(!$tMO->add($tData)){
                $this->error('提交失败'); 
            }
            $this->success('新增成功', 'index');
            exit;
        }
        $this->display();
    }
    public function edit(){
        $tTime = time(); 

        $pId = I('id' , '' , 'intval'); 
        $tMO = M('zhuanjia');
        if(empty($pId)){
            $this->error('ID不能为空');
        }

        if(IS_POST){
            $pName = I('name' , '' , 'htmlspecialchars'); 
            $pDescription = I('description' , '' , 'htmlspecialchars'); 
            $pDepartment = I('department' , '' , 'htmlspecialchars'); 
            $pIntroduction = I('introduction' , '' , 'htmlspecialchars'); 
            $pOccupation = I('occupation' , '' , 'htmlspecialchars'); 
            $pLitpic = I('litpic', '', 'htmlspecialchars,trim');

            if(empty($pName)){
                $this->error('专家名称能为空');
            }
            if(empty($pIntroduction)){
                $this->error('介绍不能为空');
            }
            $tData = array(
                'name' => $pName,
                'description' => $pDescription,
                'introduction' => $pIntroduction,
                'occupation' => $pOccupation,
                'department' => $pDepartment,
                'updated' => $tTime,
                'litpic' => $pLitpic,
            );
            $tMO = M('zhuanjia');

            if(!$tMO->where('id='.$pId)->save($tData)){
                $this->error('提交失败'); 
            }
            $this->success('修改成功', 'index');
            exit;
        }
        $tRow = $tMO->where('id='.$pId)->find();
        $this->assign('tRow' , $tRow);
        $this->display();
    }
    public function del(){
        $pId = I('id' , 0 , 'int'); 
        if(empty($pId)){
            $this->error('ID不能为空'); 
        }

        $tMO = M('zhuanjia');
        if(!$tMO->where('id = ' .$pId)->delete()){
            $this->error('删除失败'); 
        }
        $this->success('彻底删除成功', U(MODULE_NAME. '/Zhuanjia/index'));
    }
    public function delall(){
        $tIdarr = I('key',0 , 'intval');  
        if (!is_array($tIdarr)) {
            $this->error('请选择要彻底删除的项');
        }
        $tWhere = array('id' => array('in', $tIdarr));
        $tMO = M('zhuanjia');
        if(!$tMO->where($tWhere)->delete()){
            $this->error('删除失败'); 
        }

        $this->success('彻底删除成功', U(MODULE_NAME. '/Zhuanjia/index'));
    }

}
